package gitee_hook

import (
	"encoding/json"
	"fmt"
	"testing"
)

var issueData1 = `
{
    "action": "open",
    "action_desc": "open",
    "assignee": null,
    "description": "阿斯蒂芬撒地方",
    "enterprise": null,
    "hook_id": 935886,
    "hook_name": "issue_hooks",
    "hook_url": "https://gitee.com/waibaoxia/webhook/hooks/935886/edit",
    "iid": "I4XL2O",
    "issue": {
        "assignee": null,
        "body": "阿斯蒂芬撒地方",
        "collaborators": [],
        "comments": 0,
        "created_at": "2022-03-13T14:47:52+08:00",
        "html_url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
        "id": 8285424,
        "labels": [],
        "milestone": null,
        "number": "I4XL2O",
        "state": "open",
        "state_name": "待办的",
        "title": "测试1",
        "type_name": "任务",
        "updated_at": "2022-03-13T14:47:52+08:00",
        "user": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        }
    },
    "milestone": null,
    "password": "aaaaaaaaaaaaa",
    "project": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 1,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:47:52+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "push_data": null,
    "repository": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 1,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:47:52+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "sender": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "sign": "",
    "state": "open",
    "target_user": null,
    "timestamp": "1647154072806",
    "title": "测试1",
    "updated_by": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
    "user": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    }
}
`
var issueData2 = `
{
    "action": "state_change",
    "action_desc": "state_change",
    "assignee": null,
    "description": "阿斯蒂芬撒地方",
    "enterprise": null,
    "hook_id": 935886,
    "hook_name": "issue_hooks",
    "hook_url": "https://gitee.com/waibaoxia/webhook/hooks/935886/edit",
    "iid": "I4XL2O",
    "issue": {
        "assignee": null,
        "body": "阿斯蒂芬撒地方",
        "collaborators": [],
        "comments": 0,
        "created_at": "2022-03-13T14:47:52+08:00",
        "html_url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
        "id": 8285424,
        "labels": [],
        "milestone": null,
        "number": "I4XL2O",
        "state": "progressing",
        "state_name": "进行中",
        "title": "测试1",
        "type_name": "任务",
        "updated_at": "2022-03-13T14:48:39+08:00",
        "user": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        }
    },
    "milestone": null,
    "password": "aaaaaaaaaaaaa",
    "project": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:48:39+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "push_data": null,
    "repository": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:48:39+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "sender": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "sign": "",
    "state": "progressing",
    "target_user": null,
    "timestamp": "1647154119884",
    "title": "测试1",
    "updated_by": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
    "user": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    }
}`

var issueData3 = `
{
    "action": "delete",
    "action_desc": "delete",
    "assignee": null,
    "description": "阿斯蒂芬撒地方",
    "enterprise": null,
    "hook_id": 935886,
    "hook_name": "issue_hooks",
    "hook_url": "https://gitee.com/waibaoxia/webhook/hooks/935886/edit",
    "iid": "I4XL2O",
    "issue": {
        "assignee": null,
        "body": "阿斯蒂芬撒地方",
        "collaborators": [],
        "comments": 1,
        "created_at": "2022-03-13T14:47:52+08:00",
        "html_url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
        "id": 8285424,
        "labels": [],
        "milestone": null,
        "number": "I4XL2O",
        "state": "progressing",
        "state_name": "进行中",
        "title": "测试1",
        "type_name": "任务",
        "updated_at": "2022-03-13T14:49:21+08:00",
        "user": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        }
    },
    "milestone": null,
    "password": "aaaaaaaaaaaaa",
    "project": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:48:39+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "push_data": null,
    "repository": {
        "clone_url": "https://gitee.com/waibaoxia/webhook.git",
        "created_at": "2022-03-13T10:35:59+08:00",
        "default_branch": "master",
        "description": "",
        "fork": false,
        "forks_count": 0,
        "full_name": "waibaoxia/webhook",
        "git_http_url": "https://gitee.com/waibaoxia/webhook.git",
        "git_ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "git_svn_url": "svn://gitee.com/waibaoxia/webhook",
        "git_url": "git://gitee.com/waibaoxia/webhook.git",
        "has_issues": true,
        "has_pages": false,
        "has_wiki": true,
        "homepage": null,
        "html_url": "https://gitee.com/waibaoxia/webhook",
        "id": 21383091,
        "language": "Go",
        "license": "GPL-3.0",
        "name": "webhook",
        "name_with_namespace": "潘雷明/webhook",
        "namespace": "waibaoxia",
        "open_issues_count": 0,
        "owner": {
            "avatar_url": "https://gitee.com/assets/no_portrait.png",
            "email": "10295679+waibaoxia@user.noreply.gitee.com",
            "html_url": "https://gitee.com/waibaoxia",
            "id": 10295679,
            "login": "waibaoxia",
            "name": "潘雷明",
            "remark": null,
            "site_admin": false,
            "type": "User",
            "url": "https://gitee.com/waibaoxia",
            "user_name": "waibaoxia",
            "username": "waibaoxia"
        },
        "path": "webhook",
        "path_with_namespace": "waibaoxia/webhook",
        "private": true,
        "pushed_at": "2022-03-13T13:57:11+08:00",
        "ssh_url": "git@gitee.com:waibaoxia/webhook.git",
        "stargazers_count": 0,
        "svn_url": "svn://gitee.com/waibaoxia/webhook",
        "updated_at": "2022-03-13T14:48:39+08:00",
        "url": "https://gitee.com/waibaoxia/webhook",
        "watchers_count": 1
    },
    "sender": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "sign": "",
    "state": "progressing",
    "target_user": null,
    "timestamp": "1647154209971",
    "title": "测试1",
    "updated_by": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    },
    "url": "https://gitee.com/waibaoxia/webhook/issues/I4XL2O",
    "user": {
        "avatar_url": "https://gitee.com/assets/no_portrait.png",
        "email": "10295679+waibaoxia@user.noreply.gitee.com",
        "html_url": "https://gitee.com/waibaoxia",
        "id": 10295679,
        "login": "waibaoxia",
        "name": "潘雷明",
        "remark": null,
        "site_admin": false,
        "type": "User",
        "url": "https://gitee.com/waibaoxia",
        "user_name": "waibaoxia",
        "username": "waibaoxia"
    }
}
`

func TestIssueHook1(t *testing.T) {
	realTestIssue(issueData1, t)
}
func TestIssueHook2(t *testing.T) {
	realTestIssue(issueData2, t)
}

func TestIssueHook3(t *testing.T) {
	realTestIssue(issueData3, t)
}

func realTestIssue(issueData string, t *testing.T) {
	issueHook := &IssueHook{}
	err := json.Unmarshal([]byte(issueData), issueHook)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	data, _ := json.MarshalIndent(issueHook, "  ", "  ")
	fmt.Println(string(data))
}
